class TopicsController < ApplicationController
  def index
      @topics = Topic.all
  end

  def show
    @topic = Topic.find(params[:id])
  end

  def new
    @topic = Topic.new
    @post = Post.new
  end

def create
    @topic = Topic.new(params[:topic])
    @post = Post.new
    if @topic.save
        @topic = Topic.new(:name => params[:topic][:name], :nombre_autor => params[:topic][:nombre_autor], :last_post_at => Time.now, :forum_id => params[:topic][:forum_id])
        
        flash[:notice] = "Successfully created topic."
        redirect_to "/forums/#{@topic.forum_id}"
    else
        render :action => 'new'
    end
end

  def edit
    @topic = Topic.find(params[:id])
  end

  def update
    @topic = Topic.find(params[:id])
    if @topic.update_attributes(params[:topic])
      redirect_to "/forums/#{@topic.forum.id}"
    else
      render :action => 'edit'
    end
  end

  def destroy
    @topic = Topic.find(params[:id])
    @topic.destroy
     redirect_to "/forums/#{@topic.forum.id}"
  end
end
